function print_today() {

  var now = new Date();
  var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
  function fourdigits(number) {
    return (number < 1000) ? number + 1900 : number;
  }
  var today =  months[now.getMonth()] + " " + date + ", " + (fourdigits(now.getYear()));
  return today;
}

// from http://www.mediacollege.com/internet/javascript/number/round.html
function roundNumber(number,decimals) {
  var newString;// The new rounded number
  decimals = Number(decimals);
  if (decimals < 1) {
    newString = (Math.round(number)).toString();
  } else {
    var numString = number.toString();
    if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
      numString += ".";// give it one at the end
    }
    var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
    var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
    var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
    if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
      if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
        while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
          if (d1 != ".") {
            cutoff -= 1;
            d1 = Number(numString.substring(cutoff,cutoff+1));
          } else {
            cutoff -= 1;
          }
        }
      }
      d1 += 1;
    } 
    if (d1 == 10) {
      numString = numString.substring(0, numString.lastIndexOf("."));
      var roundedNum = Number(numString) + 1;
      newString = roundedNum.toString() + '.';
    } else {
      newString = numString.substring(0,cutoff) + d1.toString();
    }
  }
  if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
    newString += ".";
  }
  var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
  for(var i=0;i<decimals-decs;i++) newString += "0";
  //var newNumber = Number(newString);// make it a number if you like
  return newString; // Output the result to the form field (change for your purposes)
}

function update_total() {
  var total = 0;
  $('.price').each(function(i){
    price = $(this).html().replace("Q","");
    if (!isNaN(price)) total += Number(price);
  });

  total = roundNumber(total,2);

  $('#subtotal').html("Q"+total);
  $('#total').html("Q"+total);
  
  update_balance();
}

function update_balance() {
  var due = Number($("#total").html().replace("Q","")) + (Number($("#total").html().replace("Q","")) * (Number($("#paid").val().replace("Q",""))));
  due = roundNumber(due,2);
  var costo = Number($("#total").html().replace("Q",""));
  $('.due').html("Q"+due);
  $('.costo').val(costo);
}

function update_pricec() {
  var row = $(this).parents('.item-rowc');
  var price = row.find('.cost').val().replace("Q","") * row.find('.qty').val();
  price = roundNumber(price,2);
  isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("Q"+price);

  update_total();
  }

  function update_pricem() {
  var row = $(this).parents('.item-rowm');
  var price = row.find('.cost').val().replace("Q","") * row.find('.qty').val();
  price = roundNumber(price,2);
  isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("Q"+price);

  update_total();
  }

  function update_pricee() {
  var row = $(this).parents('.item-rowe');
  var price = row.find('.cost').val().replace("Q","") * row.find('.qty').val();
  price = roundNumber(price,2);
  isNaN(price) ? row.find('.price').html("N/A") : row.find('.price').html("Q"+price);

  update_total();
  }

function bindc() {
  $(".cost").blur(update_pricec);
  $(".qty").blur(update_pricec);
}
function bindm() {
  $(".cost").blur(update_pricem);
  $(".qty").blur(update_pricem);
}
function binde() {
  $(".cost").blur(update_pricee);
  $(".qty").blur(update_pricee);
}

$(document).ready(function() {

  $('input').click(function(){
    $(this).select();
  });

  $("#paid").blur(update_balance);

var cont = 0, contm = 0, conte = 0, conto = 0, del = 2;
$("#addrowc").click(function(){
cont += 1;
$('.contc').val(cont);
$('.nor').html("Detalle Colores ("+cont+")");
var string = '<tr class="item-rowc"><td class="item-name"><div class="delete-wpr"><textarea name="detc_id'+cont+'"></textarea><a class="deletec" href="javascript:;" title="Remove row">X</a></div></td><td class="description"><textarea name="detc_desc'+cont+'"></textarea></td><td><textarea name="detc_pu'+cont+'" class="cost"></textarea></td><td><textarea name="detc_cant'+cont+'" class="qty"></textarea></td><td><span class="price">Q</span></td></tr>';
  $(".item-rowc:last").after(string);
    if ($(".deletec").length > 0) $(".deletec").show();
    bindc();
  });

  bindc();

  $(".deletec").live('click',function(){
    cont -= 1;
    $('.contc').val(cont);
  $('.nor').html("Detalle Colores ("+cont+")");
    $(this).parents('.item-rowc').remove();
    update_total();
    if ($(".deletec").length < del) $(".deletec").hide();
  });



  $("#addrowm").click(function(){
contm += 1;
$('.contm').val(contm);
$('.norm').html("Detalle Materiales ("+contm+")");
var string = '<tr class="item-rowm"><td class="item-name"><div class="delete-wpr"><textarea name="detm_id'+contm+'"></textarea><a class="deletem" href="javascript:;" title="Remove row">X</a></div></td><td class="description"><textarea name="detm_desc'+contm+'"></textarea></td><td><textarea name="detm_pu'+contm+'" class="cost"></textarea></td><td><textarea name="detm_cant'+contm+'" class="qty"></textarea></td><td><span class="price">Q</span></td></tr>';
  $(".item-rowm:last").after(string);
    if ($(".deletem").length > 0) $(".deletem").show();
    bindm();
  });

  bindm();

  $(".deletem").live('click',function(){
    contm -= 1;
    $('.contm').val(contm);
  $('.norm').html("Detalle Materiales ("+contm+")");
    $(this).parents('.item-rowm').remove();
    update_total();
    if ($(".deletem").length < del) $(".deletem").hide();
  });




  $("#addrowe").click(function(){
conte += 1;
$('.conte').val(conte);
$('.nore').html("Detalle Extras ("+conte+")");
var string = '<tr class="item-rowe"><td class="item-name"><div class="delete-wpr"><textarea name="dete_id'+conte+'"></textarea><a class="deletee" href="javascript:;" title="Remove row">X</a></div></td><td class="description"><textarea name="dete_desc'+conte+'"></textarea></td><td><textarea name="dete_pu'+conte+'" class="cost"></textarea></td><td><textarea name="dete_cant'+conte+'" class="qty"></textarea></td><td><span class="price">Q</span></td></tr>';
  $(".item-rowe:last").after(string);
    if ($(".deletee").length > 0) $(".deletee").show();
    binde();
  });

  binde();

  $(".deletee").live('click',function(){
    conte -= 1;
    $('.conte').val(conte);
  $('.nore').html("Detalle Extras ("+conte+")");
    $(this).parents('.item-rowe').remove();
    update_total();
    if ($(".deletee").length < del) $(".deletee").hide();
  });




  $("#addrowo").click(function(){
conto += 1;
$('.conto').val(conto);
$('.noro').html("Detalle Observaciones ("+conto+")");
var string = '<tr class="item-rowo"><td class="item-name"><div class="delete-wpr"><textarea name="deto_id'+conto+'"></textarea><a class="deleteo" href="javascript:;" title="Remove row">X</a></div></td>';
  $(".item-rowo:last").after(string);
    if ($(".deleteo").length > 0) $(".deleteo").show();
    bindo();
  });

  bindo();

  $(".deleteo").live('click',function(){
    conto -= 1;
    $('.conto').val(conto);
  $('.noro').html("Detalle Observaciones ("+conto+")");
    $(this).parents('.item-rowo').remove();
    update_total();
    if ($(".deletep").length < del) $(".deleteo").hide();
  });

  $("#date").val(print_today());

});